package cn.jja8.bukkit.bukkitwindowtool.window;

import cn.jja8.bukkit.bukkitwindowtool.window.error.FlushNoWorked;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;

public class ListWindow implements Window {

    protected  ArrayList<Button> buttonList = new ArrayList<>();

    protected String name;

    protected Inventory inventory;

    public ListWindow(String name) {
        this.name = name;
    }

    /**
     * 添加一个按钮到列表中
     * */
    public void addButton(Button button){
        buttonList.add(button);
        try {
            button.beAdd(this, () -> flush(button));
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        dilatation();
        flush(button);

    }
    /**
     * 计算容量
     * */
    private int size(){
        int listSize = buttonList.size();
        int n = 1+listSize/9;
        if (n>6){
            n = 6;
        }
        return 9*n;
    }

    /**
     * 容量检查
     * */
    private void dilatation(){
        if (inventory==null){
            return;
        }
        int size = size();
        if (inventory.getMaxStackSize()<size) {
            inventory.setMaxStackSize(size);
        }
    }

    /**
     * 刷新物品
     * */
    private void flush(Button button){
        int index = buttonList.indexOf(button);
        if (index<0){
            throw new FlushNoWorked("刷新过期！");
        }
        if (inventory!=null) {
            ItemStack itemStack = null;
            try {
                itemStack = button.getItemStack();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            inventory.setItem(index, itemStack);
        }
    }





    @Override
    public Inventory getInventory() {
        if (inventory==null){
            int size = size();
            int minSize = Math.min(buttonList.size(), size);

            inventory = name==null?Bukkit.createInventory(null,size):Bukkit.createInventory(null,size,name);
            for (int i=0;i<minSize;i++) {
                Button button = buttonList.get(i);
                ItemStack itemStack = null;
                try {
                    if (button != null) {
                        itemStack = button.getItemStack();
                    }
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                if (itemStack!=null){
                    inventory.setItem(i, itemStack);
                }
            }
        }
        return inventory;
    }

    @Override
    public void click(InventoryClickEvent inventoryClickEvent,Player player) {
        int chick = inventoryClickEvent.getRawSlot();
        if (chick<0||chick>=buttonList.size()){
            inventoryClickEvent.setCancelled(true);
            return;
        }
        Button button = buttonList.get(chick);
        if (button==null){
            inventoryClickEvent.setCancelled(true);
            return;
        }
        try {
            button.click(inventoryClickEvent,player);
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
    }
}
